package com.ibm.ems.dao;

import java.sql.Connection;

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.ibm.ems.dto.LocationBean;




//Class DAO to access database

public class LocationDAO {

protected Connection conn;
	
public LocationDAO(Connection conn) {
	this.conn = conn;
}


	
	
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//To create a Topic
public void LocationDaoCreate(LocationBean t1)
{
	

	PreparedStatement pstmt = null;
	
	
	
	


//t1.setTopic_code(request.getParameter("TopicCode"));
//t1.setCategory(request.getParameter("Category"));

try{



pstmt=conn.prepareStatement("insert into location values(?,?,?,?)");	

pstmt.setString(1,t1.getLocationId());
pstmt.setString(2,t1.getLocationCity());
pstmt.setString(3,t1.getLocationState());
pstmt.setString(4,t1.getLocationDesc());

pstmt.executeQuery();



}catch(SQLException e){}
catch(Exception e)
{}


}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//To Create Topics Home Page
public void LocationDaoInit(ArrayList<LocationBean> l) 		
{
	
	PreparedStatement pstmt = null;
	ResultSet rs = null;

	try {
		;
			//ArrayList<String> l1=new ArrayList<String>();
		//ArrayList<String> l2=new ArrayList<String>();
		//ArrayList<String> l3=new ArrayList<String>();
		
		
		
		try {
			pstmt = conn.prepareStatement("select * from location");
			int count=0;
		LocationBean f=new LocationBean();
			rs = pstmt.executeQuery();
			
			
			while (rs.next())
			{System.out.println("ad");
				f.setLocationId(rs.getString("LOCATION_ID"));
				System.out.println(f.getLocationId());
				f.setLocationCity(rs.getString("LOCATION_CITY"));
				f.setLocationState(rs.getString("LOCATION_STATE"));											
				f.setLocationDesc(rs.getString("LOCATION_DESC"));
				l.add(count, f);
							
				f=new LocationBean();
				count++;
						}
				
			
				
			
			
		} 
		catch (SQLException ex)
		{
			throw ex;
		} finally {
			try {
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
			} catch (Exception ex1){
				
			}
		}
		
		
	
	
	} catch (SQLException ex) {
		// TODO Auto-generated catch block
		ex.printStackTrace();
	}
	




}
	
	
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//To Recieve data from database
public void modify(LocationBean t)
{
	
	PreparedStatement pstmt = null;
	ResultSet rs;
try{
	pstmt=conn.prepareStatement("select LOCATION_ID,LOCATION_CITY,LOCATION_STATE,LOCATION_DESC from location where LOCATION_ID=? ");
	pstmt.setString(1,t.getLocationId());
	
	rs = pstmt.executeQuery();
	
	rs.next();
	t.setLocationId(rs.getString("LOCATION_ID"));	
	t.setLocationCity(rs.getString("LOCATION_CITY"));
	t.setLocationDesc(rs.getString("LOCATION_DESC"));
	t.setLocationState(rs.getString("LOCATION_STATE"));
	
		
	
}catch(SQLException e)
{}
	
	


}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
//To modify in database
public void modifydatabase(LocationBean t)
{
	PreparedStatement pstmt = null;

	try{
		pstmt=conn.prepareStatement("update location set LOCATION_CITY=?,LOCATION_STATE=?,LOCATION_DESC=? where LOCATION_ID=? ");
		
		pstmt.setString(1,t.getLocationCity());
		pstmt.setString(2,t.getLocationState());
		pstmt.setString(3,t.getLocationDesc());
		pstmt.setString(4,t.getLocationId());
				
		
		pstmt.executeQuery();
			
	}catch(SQLException e)
	{System.out.println("dssdss");}

}
	
	

}
